package common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther lileilei
 * @create 2020-01-12 12:49
 */
public class GetComplexJson {
    private List<Object> valueList=new ArrayList<>();
    private Logger logger = LoggerFactory.getLogger(GetComplexJson.class);
    public List<Object> getValueList(String value,String key) {
        StringJsonParson stringJsonParson=new StringJsonParson();
        Map<String ,Object> valueMap=stringJsonParson.json2map(value);
        logger.info("开始解析json字符串");
        for (String string:valueMap.keySet()){
            logger.info("解析");
            if (key.equals(string)){
              if (valueMap.get(key) instanceof  ArrayList){
                  valueList.addAll(listReslut((List<Object>) valueMap.get(string),key));
              }else if (valueMap.get(key) instanceof Map){
                  valueList.addAll(getValueList(valueMap.get(string).toString(),key));
              }else{
                  if (valueMap.get(string) instanceof Integer){
                      valueList.add(new Integer(valueMap.get(string).toString()).intValue());
                  }else {
                      valueList.add((valueMap.get(string).toString()));
                  }
              }
            }else {
                if (valueMap.get(string) instanceof  ArrayList){
                    valueList.addAll(listReslut((List<Object>) valueMap.get(string),key));
                }else if (valueMap.get(string) instanceof Map){
                    valueList.add((valueMap.get(key).toString()));
                }
            }
        }
        return valueList;
    }
    private  List<Object> listReslut(List<Object> o,String value){
        List<Object> list=new ArrayList<>();
        for (Object item: o){
            if (item instanceof  Map){
                StringJsonParson stringJsonParson=new StringJsonParson();
                Map<String ,Object> stringObjectMapvalueMap=stringJsonParson.json2map(item.toString());
                list.add(stringObjectMapvalueMap.get(value).toString());
            }
        }
        return list;
    }

}
